package com.example.spider;

import com.example.util.UrlFileDownloadUtil;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class MyPipeline implements Pipeline {
    @Override
    public void process(ResultItems resultItems, Task task) {
        // System.out.println(resultItems.getRequest().getUrl());
        String imgUrl = resultItems.get("img").toString();
        String url = resultItems.getRequest().getUrl();
        String partName = "";

        Map<String, String> map = (HashMap<String, String>) resultItems.get("parts");
        for (String key : map.keySet()) {
            if(url.contains(key)){
                partName = map.get(key);
                break;
            }
        }
        UrlFileDownloadUtil.downloadPicture(imgUrl, partName);
    }
}
